package cn.stylefeng.guns.modular.rest.pandian;

import java.math.BigDecimal;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.basic.entity.BasicGoods;
import cn.stylefeng.guns.modular.basic.entity.BasicPosition;
import cn.stylefeng.guns.modular.basic.service.BasicGoodsService;
import cn.stylefeng.guns.modular.basic.service.BasicPositionService;
import cn.stylefeng.guns.modular.pandian.entity.PandianDetailed;
import cn.stylefeng.guns.modular.pandian.entity.PandianJihua;
import cn.stylefeng.guns.modular.pandian.entity.PandianJihuaDetailed;
import cn.stylefeng.guns.modular.pandian.model.params.PandianDetailedParam;
import cn.stylefeng.guns.modular.pandian.model.params.PandianJihuaDetailedParam;
import cn.stylefeng.guns.modular.pandian.model.params.PandianJihuaParam;
import cn.stylefeng.guns.modular.pandian.model.params.PandianJihuaUserParam;
import cn.stylefeng.guns.modular.pandian.model.result.PandianDetailedResult;
import cn.stylefeng.guns.modular.pandian.model.result.PandianJihuaDetailedResult;
import cn.stylefeng.guns.modular.pandian.model.result.PandianJihuaUserResult;
import cn.stylefeng.guns.modular.pandian.service.PandianDetailedService;
import cn.stylefeng.guns.modular.pandian.service.PandianJihuaDetailedService;
import cn.stylefeng.guns.modular.pandian.service.PandianJihuaService;
import cn.stylefeng.guns.modular.pandian.service.PandianJihuaUserService;
import cn.stylefeng.guns.modular.rest.aop.UserAop;
import cn.stylefeng.guns.modular.system.model.params.SysSwitchParam;
import cn.stylefeng.guns.modular.system.model.result.SysSwitchResult;
import cn.stylefeng.guns.modular.system.service.SysSwitchService;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;

/**
 * 盘点
 */
@Controller
@RequestMapping("/rest/PanDian")
public class restPanDianController extends BaseController {

    @Autowired
    private PandianJihuaService pandianJihuaService;
    
    @Autowired
    private PandianJihuaUserService pandianJihuaUserService;

    @Autowired
    private PandianDetailedService pandianDetailedService;
    
    @Autowired
    private UserAop userAop;

    @Autowired
    private BasicPositionService basicPositionService;

    @Autowired
    private BasicGoodsService basicGoodsService;
    
    @Autowired
	private SysSwitchService sysSwitchService;

    /**
     * 查询计划列表
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(PandianJihuaParam pandianJihuaParam,String condition) {
    	User user = userAop.getUser();
    	pandianJihuaParam.setDeptId(user.getDeptId());
    	pandianJihuaParam.setDeliverId(user.getDeliverId());
    	LayuiPageInfo LayuiPageInfo = this.pandianJihuaService.findPageBySpecApplets(pandianJihuaParam,condition,user.getOperateId());
    	LayuiPageInfo.setCode(200);
        return LayuiPageInfo;
    }

    /**
     * 查询待盘
     * 库区、库位列表
     */
    @ResponseBody
    @RequestMapping("/staylist")
    public ResponseData staylist(PandianJihuaUserParam pandianJihuaUserParam,String condition) {
    	User user = userAop.getUser();
    	pandianJihuaUserParam.setUserId(user.getUserId());
    	List<PandianJihuaUserResult> list = pandianJihuaUserService.findListBySpec(pandianJihuaUserParam);
    	for (PandianJihuaUserResult r : list) {
    		PandianDetailedParam pandianDetailedParam = new PandianDetailedParam();
    		pandianDetailedParam.setAreaId(r.getAreaId());
    		List<PandianDetailedResult> list2 = pandianDetailedService.findPanDianPositionListBySpec(pandianDetailedParam,r.getUserId(),pandianJihuaUserParam.getDiskId());
    		r.setList(list2);
		}
    	return ResponseData.success(list);
    }

    /**
     * 查询已盘
     * 库区、库位列表
     */
    @ResponseBody
    @RequestMapping("/yistaylist")
    public ResponseData yistaylist(PandianJihuaUserParam pandianJihuaUserParam,String condition) {
    	User user = userAop.getUser();
    	pandianJihuaUserParam.setUserId(user.getUserId());
    	List<PandianJihuaUserResult> list = pandianJihuaUserService.findListBySpec(pandianJihuaUserParam);
    	for (PandianJihuaUserResult r : list) {
    		PandianJihuaDetailedParam pandianJihuaDetailedParam = new PandianJihuaDetailedParam();
    		pandianJihuaDetailedParam.setAreaId(r.getAreaId());
    		List<PandianJihuaDetailedResult> list2 = pandianJihuaDetailedService.findPanDianPositionListBySpec(pandianJihuaDetailedParam,r.getUserId(),pandianJihuaUserParam.getDiskId());
    		r.setList2(list2);
		}
    	return ResponseData.success(list);
    }

    /**
     * 盘库详情
     * 查询已盘
     * 库区、库位列表
     */
    @ResponseBody
    @RequestMapping("/yistaylist_query")
    public ResponseData yistaylist_query(PandianJihuaUserParam pandianJihuaUserParam,String condition) {
    	List<PandianJihuaUserResult> list = pandianJihuaUserService.findListBySpec(pandianJihuaUserParam);
    	for (PandianJihuaUserResult r : list) {
    		PandianJihuaDetailedParam pandianJihuaDetailedParam = new PandianJihuaDetailedParam();
    		pandianJihuaDetailedParam.setAreaId(r.getAreaId());
    		List<PandianJihuaDetailedResult> list2 = pandianJihuaDetailedService.findPanDianPositionListBySpec(pandianJihuaDetailedParam,r.getUserId(),pandianJihuaUserParam.getDiskId());
    		r.setList2(list2);
		}
    	return ResponseData.success(list);
    }

    /**
     * 查询待盘
     * 托盘列表
     */
    @ResponseBody
    @RequestMapping("/positionlist")
    public ResponseData positionlist(Long diskId,Long positionId) {
    	User user = userAop.getUser();
		PandianDetailedParam pandianDetailedParam = new PandianDetailedParam();
		pandianDetailedParam.setPositionId(positionId);
		PandianDetailedResult pandianDetailedResult = pandianDetailedService.findPanDianPositionBySpec(pandianDetailedParam,user.getUserId(),diskId);
		if(pandianDetailedResult != null) {
			List<PandianDetailedResult> list2 = pandianDetailedService.findPanDianGoodsListBySpec(pandianDetailedParam,user.getUserId(),diskId);
			for (PandianDetailedResult r : list2) {
				pandianDetailedParam.setGoodsId(r.getGoodsId());
				List<PandianDetailedResult> list3 = pandianDetailedService.findPanDianTrayListBySpec(pandianDetailedParam,user.getUserId(),diskId);
				r.setList2(list3);
			}
			pandianDetailedResult.setList(list2);
	    	return ResponseData.success(pandianDetailedResult);
		} else {
	    	return ResponseData.success();
		}
    }

    /**
     * 查询已盘
     * 托盘列表
     */
    @ResponseBody
    @RequestMapping("/yipositionlist")
    public ResponseData yipositionlist(Long diskId,Long positionId) {
    	User user = userAop.getUser();
    	PandianJihuaDetailedParam pandianJihuaDetailedParam = new PandianJihuaDetailedParam();
    	pandianJihuaDetailedParam.setPositionId(positionId);
    	PandianJihuaDetailedResult pandianJihuaDetailedResult = pandianJihuaDetailedService.findPanDianPositionBySpec(pandianJihuaDetailedParam,user.getUserId(),diskId);
		if(pandianJihuaDetailedResult != null) {
			List<PandianJihuaDetailedResult> list2 = pandianJihuaDetailedService.findPanDianGoodsListBySpec(pandianJihuaDetailedParam,user.getUserId(),diskId);
			for (PandianJihuaDetailedResult r : list2) {
				pandianJihuaDetailedParam.setGoodsId(r.getGoodsId());
				List<PandianJihuaDetailedResult> list3 = pandianJihuaDetailedService.findPanDianTrayListBySpec(pandianJihuaDetailedParam,user.getUserId(),diskId);
				r.setList2(list3);
			}
			pandianJihuaDetailedResult.setList(list2);
	    	return ResponseData.success(pandianJihuaDetailedResult);
		} else {
	    	return ResponseData.success();
		}
    }

    /**
     * 盘库详情
     * 查询已盘
     * 托盘列表
     */
    @ResponseBody
    @RequestMapping("/yipositionlist_query")
    public ResponseData yipositionlist_query(Long diskId,Long positionId,Long createUser) {
    	PandianJihuaDetailedParam pandianJihuaDetailedParam = new PandianJihuaDetailedParam();
    	pandianJihuaDetailedParam.setPositionId(positionId);
    	PandianJihuaDetailedResult pandianJihuaDetailedResult = pandianJihuaDetailedService.findPanDianPositionBySpec(pandianJihuaDetailedParam,createUser,diskId);
		if(pandianJihuaDetailedResult != null) {
			List<PandianJihuaDetailedResult> list2 = pandianJihuaDetailedService.findPanDianGoodsListBySpec(pandianJihuaDetailedParam,createUser,diskId);
			for (PandianJihuaDetailedResult r : list2) {
				pandianJihuaDetailedParam.setGoodsId(r.getGoodsId());
				List<PandianJihuaDetailedResult> list3 = pandianJihuaDetailedService.findPanDianTrayListBySpec(pandianJihuaDetailedParam,createUser,diskId);
				r.setList2(list3);
			}
			pandianJihuaDetailedResult.setList(list2);
	    	return ResponseData.success(pandianJihuaDetailedResult);
		} else {
	    	return ResponseData.success();
		}
    }

    @Autowired
    private PandianJihuaDetailedService pandianJihuaDetailedService;

    /**
     * 编辑接口
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(PandianJihuaDetailedParam pandianJihuaDetailedParam) {
        this.pandianJihuaDetailedService.update(pandianJihuaDetailedParam);
        return ResponseData.success();
    }

    /**
     * 确定盘点
     */
    @ResponseBody
    @RequestMapping("/add")
    @Transactional(rollbackFor = Exception.class)
    public ResponseData add(PandianJihuaDetailedParam pandianJihuaDetailedParam) {
    	User user = userAop.getUser();
    	
    	PandianDetailed pandianDetailed = pandianDetailedService.getById(pandianJihuaDetailedParam.getDId());
    	if (ToolUtil.isEmpty(pandianJihuaDetailedParam.getTrueNum())) {
    		return ResponseData.error("请填写成数量");
		}
    	BigDecimal as = new BigDecimal(0);
    	BigDecimal num = new BigDecimal(pandianDetailed.getNum());
    	BigDecimal trueNum = new BigDecimal(pandianJihuaDetailedParam.getTrueNum());
    	if (trueNum.compareTo(as)==-1) {
    		return ResponseData.error("不能小于0");
		}
		
    	if(num.compareTo(trueNum) != 0 || !pandianDetailed.getGoodsbatchs().equals(pandianJihuaDetailedParam.getNewGoodsbatchs())) {
            //新增动盘单明细
        	if(trueNum.compareTo(as) == 0) {
        		pandianJihuaDetailedParam.setPanType("缺失");
        	} else if(trueNum.compareTo(num) == -1) {
        		pandianJihuaDetailedParam.setPanType("缺少");
        	} else if(trueNum.compareTo(num) == 1) {
        		pandianJihuaDetailedParam.setPanType("多出");
        	} else {
        		pandianJihuaDetailedParam.setPanType("修改批次");
        	}
    		SysSwitchParam dParam = new SysSwitchParam();
    		dParam.setDeliverId(user.getDeliverId());
    		dParam.setDeptId(user.getDeptId());
    		dParam.setCode("correct_switch");
    		SysSwitchResult dResult = sysSwitchService.findBySpec(dParam,user.getOperateId());
    		if (dResult!=null) {
    			if (ToolUtil.isNotEmpty(dResult.getOperation())) {
    				if ("Y".equals(dResult.getOperation())) {
    		    		pandianJihuaDetailedParam.setInventory("待处理");
    				} else {
    		    		pandianJihuaDetailedParam.setInventory("已盘");
    				}
    			}
    		}
    	} else {
    		pandianJihuaDetailedParam.setPanType("正常");
    		pandianJihuaDetailedParam.setInventory("已盘");
    	}
    	pandianJihuaDetailedParam.setDiskName(pandianDetailed.getDiskName());
    	pandianJihuaDetailedParam.setStockId(pandianDetailed.getStockId());
    	pandianJihuaDetailedParam.setWarehouseId(pandianDetailed.getWarehouseId());
    	pandianJihuaDetailedParam.setWarehouseName(pandianDetailed.getWarehouseName());
    	pandianJihuaDetailedParam.setAreaId(pandianDetailed.getAreaId());
    	pandianJihuaDetailedParam.setAreaName(pandianDetailed.getAreaName());
    	pandianJihuaDetailedParam.setPositionId(pandianDetailed.getPositionId());
    	pandianJihuaDetailedParam.setPositionNumber(pandianDetailed.getPositionNumber());
    	pandianJihuaDetailedParam.setTrayId(pandianDetailed.getTrayId());
    	pandianJihuaDetailedParam.setTrayNumber(pandianDetailed.getTrayNumber());
    	pandianJihuaDetailedParam.setGoodsId(pandianDetailed.getGoodsId());
    	pandianJihuaDetailedParam.setGoodsName(pandianDetailed.getGoodsName());
    	pandianJihuaDetailedParam.setGoodsNumber(pandianDetailed.getGoodsNumber());
    	
    	BasicGoods basicGoods = basicGoodsService.getById(pandianDetailed.getGoodsId());
    	pandianJihuaDetailedParam.setGoodsUnit(basicGoods.getGoodsUnit());
    	pandianJihuaDetailedParam.setGoodsSpecs(basicGoods.getGoodsSpecs());
    	
    	pandianJihuaDetailedParam.setNum(pandianDetailed.getNum());
    	pandianJihuaDetailedParam.setVolume(pandianDetailed.getVolume());
    	
    	BigDecimal goodsVolume = new BigDecimal(basicGoods.getGoodsVolume());
    	BigDecimal volume = trueNum.multiply(goodsVolume).setScale(2, BigDecimal.ROUND_HALF_DOWN);
    	pandianJihuaDetailedParam.setTrueVolume(volume.toString());
    	
    	pandianJihuaDetailedParam.setGoodsbatchs(pandianDetailed.getGoodsbatchs());
    	pandianJihuaDetailedParam.setDeliverId(user.getDeliverId());
    	pandianJihuaDetailedParam.setDeptId(user.getDeptId());
    	pandianJihuaDetailedParam.setCreateUserName(user.getName());
    	pandianJihuaDetailedParam.setCreateUser(user.getUserId());
    	pandianJihuaDetailedService.add(pandianJihuaDetailedParam);

		BigDecimal asas = new BigDecimal(100);
    	if(pandianDetailed.getInventory().equals("待盘")) {
    		//更新盘点单已盘、进度
    		PandianJihua pandianJihua = pandianJihuaService.getById(pandianJihuaDetailedParam.getDiskId());
    		
    		PandianDetailedParam param = new PandianDetailedParam();
    		param.setId(pandianDetailed.getId());
    		param.setInventory("已盘");
    		pandianDetailedService.update(param);
    		param.setDiskId(pandianJihuaDetailedParam.getDiskId());
    		PandianDetailedResult sumr = pandianDetailedService.sumByspec(param);//合计已已盘
    		
    		PandianJihuaParam jihuaParam = new PandianJihuaParam();
    		jihuaParam.setId(Integer.parseInt(pandianJihuaDetailedParam.getDiskId().toString()));
    		jihuaParam.setAlreadyNum(sumr.getNum());
    		jihuaParam.setAlreadyVolume(sumr.getVolume());
    		BigDecimal countNum = new BigDecimal(pandianJihua.getCountNum());
    		BigDecimal num2 = new BigDecimal(sumr.getNum());
    		BigDecimal speed = (num2.divide(countNum,2,BigDecimal.ROUND_HALF_DOWN)).multiply(asas).setScale(2, BigDecimal.ROUND_HALF_DOWN);//已盘/开盘总数*100 = 进度
    		jihuaParam.setSpeed(speed.toString());
    		pandianJihuaService.update(jihuaParam);
    	}
    	
    	//更新人员安排已盘、进度
    	PandianJihuaUserParam jihuaUserParam = new PandianJihuaUserParam();
    	jihuaUserParam.setDiskId(pandianJihuaDetailedParam.getDiskId());
    	jihuaUserParam.setAreaId(pandianJihuaDetailedParam.getAreaId());
    	jihuaUserParam.setUserId(user.getUserId());
    	PandianJihuaUserResult jihuaUserResult = pandianJihuaUserService.findBySpec(jihuaUserParam);
    	
    	PandianJihuaDetailedParam param = new PandianJihuaDetailedParam();
    	param.setDiskId(pandianJihuaDetailedParam.getDiskId());
    	param.setAreaId(pandianJihuaDetailedParam.getAreaId());
    	param.setCreateUser(user.getUserId());
    	PandianJihuaDetailedResult usumr = pandianJihuaDetailedService.sumByspec(param);//合计已已盘
    	
    	PandianJihuaUserParam jihuaUserParam2 = new PandianJihuaUserParam();
    	jihuaUserParam2.setId(jihuaUserResult.getId());
    	jihuaUserParam2.setAlreadyNum(usumr.getNum());
    	jihuaUserParam2.setAlreadyVolume(usumr.getVolume());
    	jihuaUserParam2.setTrueNum(usumr.getTrueNum());
    	jihuaUserParam2.setTrueVolume(usumr.getTrueVolume());
    	BigDecimal ucountNum = new BigDecimal(jihuaUserResult.getCountNum());
    	BigDecimal uNum2 = new BigDecimal(usumr.getNum());
    	BigDecimal uspeed = (uNum2.divide(ucountNum,2,BigDecimal.ROUND_HALF_DOWN)).multiply(asas).setScale(2, BigDecimal.ROUND_HALF_DOWN);//已盘/开盘总数*100 = 进度
    	jihuaUserParam2.setSpeed(uspeed.toString());
    	pandianJihuaUserService.update(jihuaUserParam2);
		return ResponseData.success();
    }

    /**
     * 批量盘点
     */
    @ResponseBody
    @RequestMapping("/batchadd")
    @Transactional(rollbackFor = Exception.class)
    public ResponseData batchadd(Long[] dids,String[] newbats,String[] truenums,Long diskId,Long areaId) {
		User user = userAop.getUser();
    	for (int i = 0; i < dids.length; i++) {
    		PandianJihuaDetailedParam pandianJihuaDetailedParam = new PandianJihuaDetailedParam();
    		pandianJihuaDetailedParam.setDiskId(diskId);
    		pandianJihuaDetailedParam.setDId(dids[i]);
    		pandianJihuaDetailedParam.setNewGoodsbatchs(newbats[i]);
    		pandianJihuaDetailedParam.setTrueNum(truenums[i]);
    		
    		PandianDetailed pandianDetailed = pandianDetailedService.getById(pandianJihuaDetailedParam.getDId());
    		if (ToolUtil.isEmpty(pandianJihuaDetailedParam.getTrueNum())) {
    			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚
    			return ResponseData.error("请填写成数量");
    		}
    		BigDecimal as = new BigDecimal(0);
    		BigDecimal trueNum = new BigDecimal(pandianJihuaDetailedParam.getTrueNum());
    		if (trueNum.compareTo(as)==-1) {
    			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚
    			return ResponseData.error("不能小于0");
    		}
    		
			pandianJihuaDetailedParam.setPanType("正常");
			pandianJihuaDetailedParam.setInventory("已盘");
    		pandianJihuaDetailedParam.setDiskName(pandianDetailed.getDiskName());
    		pandianJihuaDetailedParam.setStockId(pandianDetailed.getStockId());
    		pandianJihuaDetailedParam.setWarehouseId(pandianDetailed.getWarehouseId());
    		pandianJihuaDetailedParam.setWarehouseName(pandianDetailed.getWarehouseName());
    		pandianJihuaDetailedParam.setAreaId(pandianDetailed.getAreaId());
    		pandianJihuaDetailedParam.setAreaName(pandianDetailed.getAreaName());
    		pandianJihuaDetailedParam.setPositionId(pandianDetailed.getPositionId());
    		pandianJihuaDetailedParam.setPositionNumber(pandianDetailed.getPositionNumber());
    		pandianJihuaDetailedParam.setTrayId(pandianDetailed.getTrayId());
    		pandianJihuaDetailedParam.setTrayNumber(pandianDetailed.getTrayNumber());
    		pandianJihuaDetailedParam.setGoodsId(pandianDetailed.getGoodsId());
    		pandianJihuaDetailedParam.setGoodsName(pandianDetailed.getGoodsName());
    		pandianJihuaDetailedParam.setGoodsNumber(pandianDetailed.getGoodsNumber());
    		
    		BasicGoods basicGoods = basicGoodsService.getById(pandianDetailed.getGoodsId());
    		pandianJihuaDetailedParam.setGoodsUnit(basicGoods.getGoodsUnit());
    		pandianJihuaDetailedParam.setGoodsSpecs(basicGoods.getGoodsSpecs());
    		
    		pandianJihuaDetailedParam.setNum(pandianDetailed.getNum());
        	pandianJihuaDetailedParam.setVolume(pandianDetailed.getVolume());

        	
        	BigDecimal goodsVolume = new BigDecimal(basicGoods.getGoodsVolume());
        	BigDecimal volume = trueNum.multiply(goodsVolume).setScale(2, BigDecimal.ROUND_HALF_DOWN);
        	pandianJihuaDetailedParam.setTrueVolume(volume.toString());
    		
    		pandianJihuaDetailedParam.setGoodsbatchs(pandianDetailed.getGoodsbatchs());
    		pandianJihuaDetailedParam.setDeliverId(user.getDeliverId());
    		pandianJihuaDetailedParam.setDeptId(user.getDeptId());
    		pandianJihuaDetailedParam.setCreateUserName(user.getName());
        	pandianJihuaDetailedParam.setCreateUser(user.getUserId());
    		pandianJihuaDetailedService.add(pandianJihuaDetailedParam);
    		

        	if(pandianDetailed.getInventory().equals("待盘")) {
            	BigDecimal asas = new BigDecimal(100);
        		//更新盘点单已盘、进度
        		PandianJihua pandianJihua = pandianJihuaService.getById(pandianJihuaDetailedParam.getDiskId());
        		
        		PandianDetailedParam param = new PandianDetailedParam();
        		param.setId(pandianDetailed.getId());
        		param.setInventory("已盘");
        		pandianDetailedService.update(param);
        		param.setDiskId(pandianJihuaDetailedParam.getDiskId());
        		PandianDetailedResult sumr = pandianDetailedService.sumByspec(param);//合计已已盘
        		
        		PandianJihuaParam jihuaParam = new PandianJihuaParam();
        		jihuaParam.setId(Integer.parseInt(pandianJihuaDetailedParam.getDiskId().toString()));
        		jihuaParam.setAlreadyNum(sumr.getNum());
        		jihuaParam.setAlreadyVolume(sumr.getVolume());
        		BigDecimal countNum = new BigDecimal(pandianJihua.getCountNum());
        		BigDecimal num2 = new BigDecimal(sumr.getNum());
        		BigDecimal speed = (num2.divide(countNum,2,BigDecimal.ROUND_HALF_DOWN)).multiply(asas).setScale(2, BigDecimal.ROUND_HALF_DOWN);//已盘/开盘总数*100 = 进度
        		jihuaParam.setSpeed(speed.toString());
        		pandianJihuaService.update(jihuaParam);
        	}
		}

    	BigDecimal asas = new BigDecimal(100);
    	
    	//更新人员安排已盘、进度
    	PandianJihuaUserParam jihuaUserParam = new PandianJihuaUserParam();
    	jihuaUserParam.setDiskId(diskId);
    	jihuaUserParam.setAreaId(areaId);
    	jihuaUserParam.setUserId(user.getUserId());
    	PandianJihuaUserResult jihuaUserResult = pandianJihuaUserService.findBySpec(jihuaUserParam);

    	PandianJihuaDetailedParam param = new PandianJihuaDetailedParam();
    	param.setDiskId(diskId);
    	param.setAreaId(areaId);
    	param.setCreateUser(user.getUserId());
    	PandianJihuaDetailedResult usumr = pandianJihuaDetailedService.sumByspec(param);//合计已已盘
    	
    	PandianJihuaUserParam jihuaUserParam2 = new PandianJihuaUserParam();
    	jihuaUserParam2.setId(jihuaUserResult.getId());
    	jihuaUserParam2.setAlreadyNum(usumr.getNum());
    	jihuaUserParam2.setAlreadyVolume(usumr.getVolume());
    	jihuaUserParam2.setTrueNum(usumr.getTrueNum());
    	jihuaUserParam2.setTrueVolume(usumr.getTrueVolume());
    	BigDecimal ucountNum = new BigDecimal(jihuaUserResult.getCountNum());
    	BigDecimal uNum2 = new BigDecimal(usumr.getNum());
    	BigDecimal uspeed = (uNum2.divide(ucountNum,2,BigDecimal.ROUND_HALF_DOWN)).multiply(asas).setScale(2, BigDecimal.ROUND_HALF_DOWN);//已盘/开盘总数*100 = 进度
    	jihuaUserParam2.setSpeed(uspeed.toString());
    	pandianJihuaUserService.update(jihuaUserParam2);
		return ResponseData.success();
    }

    /**
     * 新增
     */
    @RequestMapping("/addStorage")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public ResponseData addStorage(PandianJihuaDetailedParam pandianJihuaDetailedParam) {
    	User user = userAop.getUser();
    	if (ToolUtil.isEmpty(pandianJihuaDetailedParam.getTrueNum())) {
    		return ResponseData.error("请填写成数量");
		}
    	BigDecimal as = new BigDecimal(0);
    	BigDecimal trueNum = new BigDecimal(pandianJihuaDetailedParam.getTrueNum());
    	if (trueNum.compareTo(as)==-1) {
    		return ResponseData.error("不能小于0");
		}
    	if (trueNum.compareTo(as)==0) {
    		return ResponseData.error("不能等于0");
		}

    	PandianJihua pandianJihua = pandianJihuaService.getById(pandianJihuaDetailedParam.getDiskId());
    	BasicPosition basicPosition = basicPositionService.getById(pandianJihuaDetailedParam.getPositionId());
    	pandianJihuaDetailedParam.setWarehouseId(Long.parseLong(basicPosition.getWarehouseId().toString()));
    	pandianJihuaDetailedParam.setWarehouseName(basicPosition.getWarehouseName());
    	pandianJihuaDetailedParam.setAreaId(Long.parseLong(basicPosition.getPositionAreaId().toString()));
    	pandianJihuaDetailedParam.setAreaName(basicPosition.getPositionAreaName());
    	
    	pandianJihuaDetailedParam.setDiskName(pandianJihua.getDiskName());
    	pandianJihuaDetailedParam.setNum("0");
    	pandianJihuaDetailedParam.setVolume("0");
    	pandianJihuaDetailedParam.setPanType("新增");

		SysSwitchParam dParam = new SysSwitchParam();
		dParam.setDeliverId(user.getDeliverId());
		dParam.setDeptId(user.getDeptId());
		dParam.setCode("correct_switch");
		SysSwitchResult dResult = sysSwitchService.findBySpec(dParam,user.getOperateId());
		if (dResult!=null) {
			if (ToolUtil.isNotEmpty(dResult.getOperation())) {
				if ("Y".equals(dResult.getOperation())) {
			    	pandianJihuaDetailedParam.setInventory("待处理");
				} else {
					pandianJihuaDetailedParam.setInventory("已盘");
				}
			}
		}

		BasicGoods basicGoods = basicGoodsService.getById(pandianJihuaDetailedParam.getGoodsId());
		pandianJihuaDetailedParam.setGoodsUnit(basicGoods.getGoodsUnit());
		pandianJihuaDetailedParam.setGoodsSpecs(basicGoods.getGoodsSpecs());
    	
    	BigDecimal goodsVolume = new BigDecimal(basicGoods.getGoodsVolume());
    	BigDecimal volume = trueNum.multiply(goodsVolume).setScale(2, BigDecimal.ROUND_HALF_DOWN);
    	pandianJihuaDetailedParam.setTrueVolume(volume.toString());
    	
    	pandianJihuaDetailedParam.setDeliverId(user.getDeliverId());
    	pandianJihuaDetailedParam.setDeptId(user.getDeptId());
    	pandianJihuaDetailedParam.setCreateUser(user.getUserId());
    	pandianJihuaDetailedParam.setCreateUserName(user.getName());
		pandianJihuaDetailedService.add(pandianJihuaDetailedParam);

    	BigDecimal asas = new BigDecimal(100);
    	
    	//更新人员安排已盘、进度
    	PandianJihuaUserParam jihuaUserParam = new PandianJihuaUserParam();
    	jihuaUserParam.setDiskId(pandianJihuaDetailedParam.getDiskId());
    	jihuaUserParam.setAreaId(pandianJihuaDetailedParam.getAreaId());
    	jihuaUserParam.setUserId(user.getUserId());
    	PandianJihuaUserResult jihuaUserResult = pandianJihuaUserService.findBySpec(jihuaUserParam);

    	PandianJihuaDetailedParam param = new PandianJihuaDetailedParam();
    	param.setDiskId(pandianJihuaDetailedParam.getDiskId());
    	param.setAreaId(pandianJihuaDetailedParam.getAreaId());
    	param.setCreateUser(user.getUserId());
    	PandianJihuaDetailedResult usumr = pandianJihuaDetailedService.sumByspec(param);//合计已已盘
    	
    	PandianJihuaUserParam jihuaUserParam2 = new PandianJihuaUserParam();
    	jihuaUserParam2.setId(jihuaUserResult.getId());
    	jihuaUserParam2.setAlreadyNum(usumr.getNum());
    	jihuaUserParam2.setAlreadyVolume(usumr.getVolume());
    	jihuaUserParam2.setTrueNum(usumr.getTrueNum());
    	jihuaUserParam2.setTrueVolume(usumr.getTrueVolume());
    	BigDecimal ucountNum = new BigDecimal(jihuaUserResult.getCountNum());
    	BigDecimal uNum2 = new BigDecimal(usumr.getNum());
    	BigDecimal uspeed = (uNum2.divide(ucountNum,2,BigDecimal.ROUND_HALF_DOWN)).multiply(asas).setScale(2, BigDecimal.ROUND_HALF_DOWN);//已盘/开盘总数*100 = 进度
    	jihuaUserParam2.setSpeed(uspeed.toString());
    	pandianJihuaUserService.update(jihuaUserParam2);
		return ResponseData.success();
    }

    /**
     * 编辑接口
     */
    @RequestMapping("/editStorage")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public ResponseData editStorage(PandianJihuaDetailedParam pandianJihuaDetailedParam) {
    	User user = userAop.getUser();
    	
    	PandianJihuaDetailed pandianJihuaDetailed = pandianJihuaDetailedService.getById(pandianJihuaDetailedParam.getId());
    	pandianJihuaDetailed.setGoodsbatchs(pandianJihuaDetailedParam.getGoodsbatchs());
    	pandianJihuaDetailed.setNewGoodsbatchs(pandianJihuaDetailedParam.getNewGoodsbatchs());
    	pandianJihuaDetailed.setTrueNum(pandianJihuaDetailedParam.getTrueNum());
    	pandianJihuaDetailed.setNodes(pandianJihuaDetailedParam.getNodes());
    	if (ToolUtil.isEmpty(pandianJihuaDetailedParam.getTrueNum())) {
    		return ResponseData.error("请填写成数量");
		}
    	BigDecimal as = new BigDecimal(0);
    	BigDecimal num = new BigDecimal(pandianJihuaDetailed.getNum());
    	BigDecimal trueNum = new BigDecimal(pandianJihuaDetailedParam.getTrueNum());
    	if (trueNum.compareTo(as)==-1) {
    		return ResponseData.error("不能小于0");
		}
		
    	if(!pandianJihuaDetailed.getPanType().equals("新增")) {
    		if(num.compareTo(trueNum) != 0 || !pandianJihuaDetailed.getGoodsbatchs().equals(pandianJihuaDetailedParam.getNewGoodsbatchs())) {
    			//新增动盘单明细
    			if(trueNum.compareTo(as) == 0) {
    				pandianJihuaDetailed.setPanType("缺失");
    			} else if(trueNum.compareTo(num) == -1) {
    				pandianJihuaDetailed.setPanType("缺少");
    			} else if(trueNum.compareTo(num) == 1) {
    				pandianJihuaDetailed.setPanType("多出");
    			} else {
    				pandianJihuaDetailed.setPanType("修改批次");
    			}
    			SysSwitchParam dParam = new SysSwitchParam();
    			dParam.setDeliverId(user.getDeliverId());
    			dParam.setDeptId(user.getDeptId());
    			dParam.setCode("correct_switch");
    			SysSwitchResult dResult = sysSwitchService.findBySpec(dParam,user.getOperateId());
    			if (dResult!=null) {
    				if (ToolUtil.isNotEmpty(dResult.getOperation())) {
    					if ("Y".equals(dResult.getOperation())) {
    						pandianJihuaDetailed.setInventory("待处理");
    					} else {
    						pandianJihuaDetailed.setInventory("已盘");
    					}
    				}
    			}
    		} else {
    			pandianJihuaDetailed.setPanType("正常");
    			pandianJihuaDetailed.setInventory("已盘");
    		}
    	} else {
        	if (trueNum.compareTo(as)==0) {
        		return ResponseData.error("不能等于0");
    		}
    	}
    	BasicGoods basicGoods = basicGoodsService.getById(pandianJihuaDetailed.getGoodsId());
    	
    	BigDecimal goodsVolume = new BigDecimal(basicGoods.getGoodsVolume());
    	BigDecimal volume = trueNum.multiply(goodsVolume).setScale(2, BigDecimal.ROUND_HALF_DOWN);
    	pandianJihuaDetailed.setTrueVolume(volume.toString());
    	
    	ToolUtil.copyProperties(pandianJihuaDetailed, pandianJihuaDetailedParam);
    	pandianJihuaDetailedService.update(pandianJihuaDetailedParam);
    	
    	BigDecimal asas = new BigDecimal(100);
    	//更新人员安排已盘、进度
    	PandianJihuaUserParam jihuaUserParam = new PandianJihuaUserParam();
    	jihuaUserParam.setDiskId(pandianJihuaDetailedParam.getDiskId());
    	jihuaUserParam.setAreaId(pandianJihuaDetailedParam.getAreaId());
    	jihuaUserParam.setUserId(user.getUserId());
    	PandianJihuaUserResult jihuaUserResult = pandianJihuaUserService.findBySpec(jihuaUserParam);

    	PandianJihuaDetailedParam param = new PandianJihuaDetailedParam();
    	param.setDiskId(pandianJihuaDetailedParam.getDiskId());
    	param.setAreaId(pandianJihuaDetailedParam.getAreaId());
    	param.setCreateUser(user.getUserId());
    	PandianJihuaDetailedResult usumr = pandianJihuaDetailedService.sumByspec(param);//合计已已盘
    	
    	if (usumr != null) {
        	PandianJihuaUserParam jihuaUserParam2 = new PandianJihuaUserParam();
        	jihuaUserParam2.setId(jihuaUserResult.getId());
        	jihuaUserParam2.setAlreadyNum(usumr.getNum());
        	jihuaUserParam2.setAlreadyVolume(usumr.getVolume());
        	jihuaUserParam2.setTrueNum(usumr.getTrueNum());
        	jihuaUserParam2.setTrueVolume(usumr.getTrueVolume());
        	BigDecimal ucountNum = new BigDecimal(jihuaUserResult.getCountNum());
        	BigDecimal uNum2 = new BigDecimal(usumr.getNum());
        	BigDecimal uspeed = (uNum2.divide(ucountNum,2,BigDecimal.ROUND_HALF_DOWN)).multiply(asas).setScale(2, BigDecimal.ROUND_HALF_DOWN);//已盘/开盘总数*100 = 进度
        	jihuaUserParam2.setSpeed(uspeed.toString());
        	pandianJihuaUserService.update(jihuaUserParam2);
		} else {
	    	PandianJihuaUserParam jihuaUserParam2 = new PandianJihuaUserParam();
	    	jihuaUserParam2.setId(jihuaUserResult.getId());
	    	jihuaUserParam2.setAlreadyNum("0");
	    	jihuaUserParam2.setAlreadyVolume("0");
	    	jihuaUserParam2.setTrueNum("0");
	    	jihuaUserParam2.setTrueVolume("0");
	    	jihuaUserParam2.setSpeed("0");
	    	pandianJihuaUserService.update(jihuaUserParam2);
		}
        return ResponseData.success();
    }

    /**
     * 删除接口
     */
    @RequestMapping("/deletetask")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public ResponseData deletetask(PandianJihuaDetailedParam pandianJihuaDetailedParam) {
    	User user = userAop.getUser();
    	BigDecimal asas = new BigDecimal(100);
    	PandianJihuaDetailed pandianJihuaDetailed = pandianJihuaDetailedService.getById(pandianJihuaDetailedParam.getId());
        this.pandianJihuaDetailedService.delete(pandianJihuaDetailedParam);
        
        if(pandianJihuaDetailed.getDId() != null) {
    		//更新盘点单已盘、进度
        	PandianJihuaDetailedParam detailedParam = new PandianJihuaDetailedParam();
        	detailedParam.setDId(pandianJihuaDetailed.getDId());
        	PandianJihuaDetailedResult detailedResult = pandianJihuaDetailedService.findBySpec(detailedParam);
        	if(detailedResult == null) {
        		PandianDetailedParam pandianDetailedParam = new PandianDetailedParam();
        		pandianDetailedParam.setId(Integer.parseInt(pandianJihuaDetailed.getDId().toString()));
        		pandianDetailedParam.setInventory("待盘");
        		pandianDetailedService.update(pandianDetailedParam);
        		
        		PandianJihua pandianJihua = pandianJihuaService.getById(pandianJihuaDetailed.getDiskId());
        		
        		pandianDetailedParam.setInventory("已盘");
        		pandianDetailedParam.setDiskId(pandianJihuaDetailed.getDiskId());
        		PandianDetailedResult sumr = pandianDetailedService.sumByspec(pandianDetailedParam);//合计已已盘
        		
        		PandianJihuaParam jihuaParam = new PandianJihuaParam();
        		jihuaParam.setId(Integer.parseInt(pandianJihuaDetailed.getDiskId().toString()));
        		if(sumr == null) {
            		jihuaParam.setAlreadyNum("0");
            		jihuaParam.setAlreadyVolume("0");
            		jihuaParam.setSpeed("0");
            		pandianJihuaService.update(jihuaParam);
        		} else {
        			jihuaParam.setAlreadyNum(sumr.getNum());
        			jihuaParam.setAlreadyVolume(sumr.getVolume());
        			BigDecimal countNum = new BigDecimal(pandianJihua.getCountNum());
        			BigDecimal num2 = new BigDecimal(sumr.getNum());
        			BigDecimal speed = (num2.divide(countNum,2,BigDecimal.ROUND_HALF_DOWN)).multiply(asas).setScale(2, BigDecimal.ROUND_HALF_DOWN);//已盘/开盘总数*100 = 进度
        			jihuaParam.setSpeed(speed.toString());
        			pandianJihuaService.update(jihuaParam);
        		}
        	}
        }
    	
    	//更新人员安排已盘、进度
    	PandianJihuaUserParam jihuaUserParam = new PandianJihuaUserParam();
    	jihuaUserParam.setDiskId(pandianJihuaDetailed.getDiskId());
    	jihuaUserParam.setAreaId(pandianJihuaDetailed.getAreaId());
    	jihuaUserParam.setUserId(user.getUserId());
    	PandianJihuaUserResult jihuaUserResult = pandianJihuaUserService.findBySpec(jihuaUserParam);

    	PandianJihuaDetailedParam param = new PandianJihuaDetailedParam();
    	param.setDiskId(pandianJihuaDetailed.getDiskId());
    	param.setAreaId(pandianJihuaDetailed.getAreaId());
    	param.setCreateUser(user.getUserId());
    	PandianJihuaDetailedResult usumr = pandianJihuaDetailedService.sumByspec(param);//合计已已盘
    	
    	if (usumr != null) {
        	PandianJihuaUserParam jihuaUserParam2 = new PandianJihuaUserParam();
        	jihuaUserParam2.setId(jihuaUserResult.getId());
        	jihuaUserParam2.setAlreadyNum(usumr.getNum());
        	jihuaUserParam2.setAlreadyVolume(usumr.getVolume());
        	jihuaUserParam2.setTrueNum(usumr.getTrueNum());
        	jihuaUserParam2.setTrueVolume(usumr.getTrueVolume());
        	BigDecimal ucountNum = new BigDecimal(jihuaUserResult.getCountNum());
        	BigDecimal uNum2 = new BigDecimal(usumr.getNum());
        	BigDecimal uspeed = (uNum2.divide(ucountNum,2,BigDecimal.ROUND_HALF_DOWN)).multiply(asas).setScale(2, BigDecimal.ROUND_HALF_DOWN);//已盘/开盘总数*100 = 进度
        	jihuaUserParam2.setSpeed(uspeed.toString());
        	pandianJihuaUserService.update(jihuaUserParam2);
		} else {
	    	PandianJihuaUserParam jihuaUserParam2 = new PandianJihuaUserParam();
	    	jihuaUserParam2.setId(jihuaUserResult.getId());
	    	jihuaUserParam2.setAlreadyNum("0");
	    	jihuaUserParam2.setAlreadyVolume("0");
	    	jihuaUserParam2.setTrueNum("0");
	    	jihuaUserParam2.setTrueVolume("0");
	    	jihuaUserParam2.setSpeed("0");
	    	pandianJihuaUserService.update(jihuaUserParam2);
		}
        return ResponseData.success();
    }

}
